package com.fz.us.oa.web.controller.com;

import com.fz.us.admin.base.bean.search.rule.Rule;
import com.fz.us.admin.base.bean.search.rule.RuleHelper;
import com.fz.us.admin.company.entity.User;
import com.fz.us.admin.dict.entity.DictItem;
import com.fz.us.oa.core.service.com.DirectorService;
import com.fz.us.oa.web.controller.BaseController;
import com.fz.us.oa.web.shiro.bind.annotation.CurrentUser;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dzw on 2018/3/20.
 */
@Controller
@RequestMapping(value = "/com/dirStat")
public class DireStatisticController extends BaseController {
    private static final long serialVersionUID = -6338017902128417978L;

    @Autowired
    private DirectorService directorService;

    /**
     * 拼接柱状图map
     * @param xNameList x轴名称列表
     * @param yNumList 具体数值列表
     * @param titleText 标题
     * @return
     */
    public Map<String, Object> getBarMap(List<String> xNameList, List<Integer> yNumList, String titleText){
        Map<String, Object> option = new HashMap<String, Object>();
        option.put("color", new String[]{"#3398DB"});

        //鼠标移动时的效果
        Map<String, Object> tooltip = new HashMap<String, Object>();
        Map<String, Object> axisPointer = new HashMap<String, Object>();
        axisPointer.put("type", "shadow");
        tooltip.put("trigger", "axis");
        tooltip.put("axisPointer", axisPointer);
        option.put("tooltip", tooltip);

        //整体位置
        Map<String, Object> grid = new HashMap<String, Object>();
        grid.put("left", "3%");
        grid.put("right", "4%");
        grid.put("bottom", "3%");
        grid.put("containLabel", true);
        option.put("grid", grid);

        //x轴
        List<Map<String, Object>> xAxis = new ArrayList<Map<String, Object>>();
        Map<String, Object> xAxixData = new HashMap<String, Object>();
        xAxixData.put("type", "category");
        xAxixData.put("data", xNameList);
        Map<String, Object> axisTick = new HashMap<String, Object>();
        axisTick.put("alignWithLabel", true);
        xAxixData.put("axisTick", axisTick);
        xAxis.add(xAxixData);
        option.put("xAxis", xAxis);

        //y轴
        List<Map<String, Object>> yAxis = new ArrayList<Map<String, Object>>();
        Map<String, Object> yAxisData = new HashMap<String, Object>();
        yAxisData.put("type", "value");
        yAxis.add(yAxisData);
        option.put("yAxis", yAxis);

        //具体数值
        List<Map<String, Object>> series = new ArrayList<Map<String, Object>>();
        Map<String, Object> seriesData = new HashMap<String, Object>();
        seriesData.put("type", "bar");
        seriesData.put("barWidth", "60%");
        seriesData.put("data", yNumList);
        //柱状图上显示数值
        Map<String, Object> label = new HashMap<String, Object>();
        Map<String, Object> normal = new HashMap<String, Object>();
        normal.put("show", true);
        normal.put("position", "top");
        label.put("normal", normal);
        seriesData.put("label", label);
        series.add(seriesData);
        option.put("series", series);

        //标题
        Map<String, Object> title = new HashMap<String, Object>();
        title.put("text", titleText);
        title.put("left", "center");
        title.put("top", 10);
        option.put("title", title);

        return option;
    }

    /**
     * 拼接饼图map
     * @param xNameList 名字列表
     * @param pieDataList 具体数据map列表
     * @param titleText 标题
     * @return
     */
    public Map<String, Object> getPieMap(List<String> xNameList, List<Map<String, Object>> pieDataList, String titleText){
        Map<String, Object> option = new HashMap<String, Object>();

        Map<String, Object> title = new HashMap<String, Object>();
        title.put("text", titleText);
        title.put("x", "center");
        title.put("top", 10);
        option.put("title", title);

        Map<String, Object> tooltip = new HashMap<String, Object>();
        tooltip.put("trigger", "item");
        tooltip.put("formatter", "{a} <br/>{b} : {c} ({d}%)"); //json传大括号
        option.put("tooltip", tooltip);

        Map<String, Object> legend = new HashMap<String, Object>();
        legend.put("orient", "vertical");
        legend.put("left", "left");
        legend.put("data", xNameList);
        option.put("legend", legend);

        List<Map<String, Object>> series = new ArrayList<Map<String, Object>>();
        Map<String, Object> seriesDataMap = new HashMap<String, Object>();
        seriesDataMap.put("name", titleText);
        seriesDataMap.put("type", "pie");
        seriesDataMap.put("radius", "55%");
        seriesDataMap.put("center", new String[]{"50%","50%"});
        seriesDataMap.put("data", pieDataList);
        Map<String, Object> itemStyle = new HashMap<String, Object>();
        Map<String, Object> emphasis = new HashMap<String, Object>();
        emphasis.put("shadowBlur", 10);
        emphasis.put("shadowOffsetX", 0);
        emphasis.put("shadowColor", "'rgba(0, 0, 0, 0.5)");
        itemStyle.put("emphasis", emphasis);
        seriesDataMap.put("itemStyle", itemStyle);
        series.add(seriesDataMap);
        option.put("series", series);

        return option;
    }

    /**
     * 学历-全区 统计图 （streetId不为空的话就是 学历-街道）
     */
    @RequestMapping(value = "eduAll", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String eduAll(@CurrentUser User user, Model model, String streetId){
        List<DictItem> eduList = getDictItems(user, "edu");

        DictItem street = null;
        String streetName = "";
        if(StringUtils.isNotEmpty(streetId)){
            street = dictItemService.get(streetId);
            streetName=street!=null ? street.getName() : "";
        }

        //柱状图数据
        List<String> xNameList = new ArrayList<String>();
        List<Integer> seriesDataList = new ArrayList<Integer>();

        //饼图数据
        List<Map<String, Object>> pieDataList = new ArrayList<Map<String, Object>>();
        Map<String, Object> pieDataMap;

        //区学历分布
        xNameList = new ArrayList<String>();
        seriesDataList = new ArrayList<Integer>();
        pieDataList = new ArrayList<Map<String, Object>>();
        for(DictItem edu:eduList){
            xNameList.add(edu.getName()); //横坐标名字
            int count;
            if(StringUtils.isEmpty(streetId)) {
                count = directorService.getCountEduAll(edu);
            }else{
                count = directorService.getCountEduAll(edu,street);
            }
            seriesDataList.add(count);

            pieDataMap = new HashMap<String, Object>();
            pieDataMap.put("value", count);
            pieDataMap.put("name", edu.getName());
            pieDataList.add(pieDataMap);
        }
        Map<String, Object> quBar = getBarMap(xNameList, seriesDataList, streetName+"主任学历分布");
        Map<String, Object> quPie = getPieMap(xNameList, pieDataList, streetName+"主任学历分布比例");
        model.addAttribute("quBar", JSONObject.fromObject(quBar));
        model.addAttribute("quPie", JSONObject.fromObject(quPie));

        if(StringUtils.isNotEmpty(streetId)){
            return "/dirStat/edu_street_stat";
        }else{
            return "/dirStat/edu_all_stat";
        }
    }


    /**
     * 性别年龄-全区 统计图 （streetId不为空的话就是 性别年龄-街道） （方法名字是年龄 其实统计的是性别）
     */
    @RequestMapping(value = "ageAll", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String ageAll(@CurrentUser User user, Model model, String streetId){

        DictItem street = null;
        String streetName = "";
        if(StringUtils.isNotEmpty(streetId)){
            street = dictItemService.get(streetId);
            streetName = street!=null ? street.getName() : "";
        }

        //柱状图数据
        List<String> xNameList = new ArrayList<String>();
        List<Integer> yNumList = new ArrayList<Integer>();

        //饼图数据
        List<Map<String, Object>> pieDataList = new ArrayList<Map<String, Object>>();
        Map<String, Object> pieDataMap = new HashMap<String, Object>();


        //小计
        int nan = directorService.getCountByGender("男");
        int nv = directorService.getCountByGender("女");
        xNameList.add("男");
        xNameList.add("女");
        yNumList.add(nan);
        yNumList.add(nv);
        pieDataMap = new HashMap<String, Object>();
        pieDataMap.put("value", nan);
        pieDataMap.put("name", "男");
        pieDataList.add(pieDataMap);
        pieDataMap = new HashMap<String, Object>();
        pieDataMap.put("value", nv);
        pieDataMap.put("name", "女");
        pieDataList.add(pieDataMap);

        Map<String, Object> quBar = getBarMap(xNameList, yNumList, streetName+"主任性别分布");
        Map<String, Object> quPie = getPieMap(xNameList, pieDataList, streetName+"主任性别分布比例");
        model.addAttribute("quBar", JSONObject.fromObject(quBar));
        model.addAttribute("quPie", JSONObject.fromObject(quPie));

        if(StringUtils.isNotEmpty(streetId)){
            return "/dirStat/age_street_stat";
        }else{
            return "/dirStat/age_all_stat";
        }
    }

    /**
     * 党员性质-全区 统计图 （streetId不为空的话就是 人员性质-街道）
     */
    @RequestMapping(value = "natureAll", method = RequestMethod.GET, produces = MediaType.TEXT_HTML_VALUE)
    public String natureAll(@CurrentUser User user, Model model, String streetId){
        List<DictItem> personNature = getDictItems(user, "directorPersonNature");

        DictItem street = null;
        String streetName = "";
        if(StringUtils.isNotEmpty(streetId)){
            street = dictItemService.get(streetId);
            streetName = street!=null ? street.getName() : "";
        }

        //柱状图数据
        List<String> xNameList = new ArrayList<String>();
        List<Integer> yNumList = new ArrayList<Integer>();

        //饼图数据
        List<Map<String, Object>> pieDataList = new ArrayList<Map<String, Object>>();
        Map<String, Object> pieDataMap;


        int allCount = 0;
        if(street==null){
            allCount = directorService.getAllCount();
        }else{
            allCount = directorService.getCountByStreet(street);
        }

        List<Rule> rules = new ArrayList<Rule>();
        if(StringUtils.isNotEmpty(streetId)){
            rules.add(RuleHelper.eq("street", dictItemService.get(streetId)));
        }
        rules.add(RuleHelper.isNotNull("joinPartyDate"));
        int dangyuanCount = directorService.findPerson(rules).size(); //党员数量
        int feidangyuanCount = allCount - dangyuanCount; //非党员数量

        pieDataMap = new HashMap<String, Object>();
        xNameList.add("党员");
        yNumList.add(dangyuanCount);

        pieDataMap.put("value", dangyuanCount);
        pieDataMap.put("name", "党员");
        pieDataList.add(pieDataMap);


        pieDataMap = new HashMap<String, Object>();
        xNameList.add("非党员");
        yNumList.add(feidangyuanCount);

        pieDataMap.put("value", feidangyuanCount);
        pieDataMap.put("name", "非党员");
        pieDataList.add(pieDataMap);


        Map<String, Object> quBar = getBarMap(xNameList, yNumList, streetName+"主任党员性质分布");
        Map<String, Object> quPie = getPieMap(xNameList, pieDataList, streetName+"主任党员性质比例");
        model.addAttribute("quBar", JSONObject.fromObject(quBar));
        model.addAttribute("quPie", JSONObject.fromObject(quPie));

        if(StringUtils.isNotEmpty(streetId)){
            return "/dirStat/nature_street_stat";
        }else{
            return "/dirStat/nature_all_stat";
        }

    }

}
